package de.koller.planet2D;

import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.tree.ISea3;
import de.koller.oceanFramework.tree.Mother;
import de.koller.oceanFramework.tree.Sea3X;

public class PNode extends Sea3X {
	
	@Mother public PRoot proot;
	
	private void threadLockCheck( String methodname ) {
		if( proot == null && !this.isInitialized() )
			return;
		if( !proot.thread.getThread().isAlive() )
			return;
		if( Thread.currentThread() == proot.thread.getThread() )
			return;
		
		throw new OceanException( "Ausführung im falschen Thread! \n" +
				"Ausgeführt in '" + Thread.currentThread() + "' sollte aber in '" +
				proot.thread.getThread() + "'!\n" +
				"In '" + this.getClass() + "#" + methodname + "' wurde dies verursacht." );
	}
	
	@Override
	protected void init() {
		super.init();
			
		threadLockCheck( "init" );
	}

	@Override
	protected void release() {
		super.release();
		
		threadLockCheck( "release" );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		threadLockCheck( "render" );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		threadLockCheck( "update" );
	}

	@Override
	public synchronized void addChild( ISea3 child ) {
		if( ((PNode)child).isInitialized() )
			throw new OceanException( "initialized nodes can't be added!" );
			
		super.addChild( child );
		
		threadLockCheck( "addChild" );
	}

	@Override
	protected void postChildInit() {
		super.postChildInit();
		
		threadLockCheck( "postChildInit" );
	}

	@Override
	public void postChildRender() {
		super.postChildRender();
		
		threadLockCheck( "postChildRender" );
	}

	@Override
	public void postChildUpdate( int elapsedTime ) {
		super.postChildUpdate( elapsedTime );
		
		threadLockCheck( "postChildUpdate" );
	}

	@Override
	public void removeChild( ISea3 child ) {
		super.removeChild( child );
		
		threadLockCheck( "removeChild" );
	}

	@Override
	public void removeMe() {
		super.removeMe();
		
		threadLockCheck( "removeMe" );
	}
	
	
}
